/*
  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
  TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  PARTICULAR PURPOSE.

  Copyright (c) 2003-2009 PRO ACTIVE SAS - www.proactive-group.com
   
  Ref_ShowUid.c
  -------------

  This is the reference applications that shows how to poll the reader,
  showing each incoming tag identifier
  
  JDA 21/11/2003 : initial release
  JDA 04/07/2006 : added type B support
  JDA 10/01/2007 : fixed a few warnings when compiling under GCC

*/
#include "products/springprox/springprox.h"

#ifdef WIN32
#include <conio.h>              /* For kbhit and getch */
#endif
#ifdef __linux
#include <unistd.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{
  SWORD rc;
  int i;
  char s_buffer[64];
  BYTE atq[2];
  BYTE atq_b[11];
  BYTE sak[1];
  BYTE uid[12];
  BYTE uid_len = 12;
  BYTE mode = 1;

  char *device = NULL;

  if (argc > 1)
    device = argv[1];

  printf("SpringCard SDK for K531/SpringProx-CF/CSB Legacy\n");
  printf("\n");
  printf("Find_Old : multi-protocol card polling reference demo\n");
  printf("-----------------------------------------------------\n");
  printf("www.springcard.com\n\n");

  /* Open reader */
  /* ----------- */

  rc = SPROX_ReaderOpen(device);
  if (rc != MI_OK)
  {
    printf("Reader not found\n");
    goto done;
  }

  rc = SPROX_ReaderGetDevice(s_buffer, sizeof(s_buffer));
  if (rc == MI_OK)
    printf("Reader found on %s\n", s_buffer);

  rc = SPROX_ReaderGetFirmware(s_buffer, sizeof(s_buffer));
  if (rc == MI_OK)
    printf("Reader firwmare is %s\n", s_buffer);

  /* Set RF field to ON */
  SPROX_ControlRF(TRUE);

  mode = 1;
#ifdef WIN32
  printf("Waiting for tags... (Press any key to exit)\n\n");
#else
  printf("Waiting for tags... (Press <Ctrl>+C to exit)\n\n");
#endif
  for (;;)
  {
    rc = MI_OK;
    /* Not too often... */
#ifdef WIN32
    if (kbhit())
      break;
    Sleep(50);
#endif
#ifdef __linux
    usleep(5000);
#endif

    /* Switch OFF Yellow and Green LEDs, Red LED is blinking */
    SPROX_ControlLedY(2, 0, 0);

    /* ISO 14443-A (and Mifare) request+anticollision+select loop */
    /* ---------------------------------------------------------- */
    for (;;)
    {
      uid_len = sizeof(uid);
      rc = SPROX_A_SelectIdle(atq, uid, &uid_len, sak);
      if (rc == MI_NOTAGERR)
        break;                  /* No more tag (or no tag at all) */
      if (rc != MI_OK)
      {
        /* Other error */
        printf("\nISO 14443-A activation error (%d)\n", rc);
        if (rc < -128)
          goto close;           /* Fatal error */
        break;
      }

      /* Green LED on */
      SPROX_ControlLedY(0, 1, 0);

      printf("Tag (A) found: UID=");
      for (i = 0; i < uid_len; i++)
        printf("%02X", uid[i]);
      printf(" ATQ=%02X%02X SAK=%02X\n", atq[1], atq[0], sak[0]);

      /* Halt currently active tag */
      rc = SPROX_A_Halt();
      if (rc != MI_OK)
      {
        printf("\nFailed to halt the ISO 14443-A tag (%d)\n", rc);
        if (rc < -128)
          goto close;           /* Fatal error */
      }
    }

    /* ISO 14443-B without anticollision */
    /* --------------------------------- */
    rc = SPROX_B_SelectIdle(0x00, atq_b);
    
	  if (rc == MI_NOTAGERR)
  	  continue;    /* No tag */

	  if (rc != MI_OK)
	  {
	    /* Other error */
	    printf("\nISO 14443-B activation error (%d)\n", rc);
	    if (rc < -128)
	      goto close;             /* Fatal error */
      continue;
    }

	  /* Yellow LED on */
	  SPROX_ControlLedY(0, 0, 1);
	
	  printf("Tag (B) found: ATQB=");
	  for (i = 0; i < 11; i++)
	    printf("%02X", atq_b[i]);
	  printf("\n");

	  /* Halt currently active tag */
  	rc = SPROX_B_Halt(atq_b);
	  if (rc != MI_OK)
	  {
	    printf("\nFailed to halt the ISO 14443-B tag (%d)\n", rc);
	    if (rc < -128)
	      goto close;             /* Fatal error */
      continue;
	  }
  }

close:
  /* Close the reader */
  SPROX_ControlRF(FALSE);
  SPROX_ReaderClose();

done:
  /* Display last error */
  if (rc == MI_OK)
  {
    printf("Done\n");
  } else
  {
    printf("%s (%d)\n", SPROX_GetErrorMessage(rc), rc);
  }
  return 0;
}
